// pages/hotExchange/hotExchange.js
const service = require('./hotExchangeService.js');
const dateUtils = require('../../../utils/dateUtils.js');
const utilService = require('../../../utils/utilService.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasNextPage: true,
    startPage: 0,
    selectIdx: 0,// 
    data: [],// timeList
    times: [],//期次
    status: [],//0已结束 1抢购中 2待开始,
    goods: [],
    shareMsg: "",
    scrollTop: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    //授权
    self.authAlert = self.selectComponent("#authAlert");
    self.authAlert.bindCatch()
    app.loginPromise.then(() => {
      self.getShareMessage(16)
    });

  },
  authSuccess() {
    var options = this.data.myOptions
    this.getShareMessage(16)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this._requestTimeList();
  },

  /**
   * 请求时间列表
   */
  _requestTimeList: function (callback) {
    let that = this;
    service.getRobTimeList(function (data) {
      if (data) {
        for (let i = 0; i < data.length; i++) {
          that.data.times[i] = dateUtils.timesToTime2(dateUtils.timeToTimestamp(data[i]) / 1000);
          if (dateUtils.timeToTimestamp(data[i]) > dateUtils.getNowTime()) {
            that.data.status[i] = 2;
          } else if ((dateUtils.getNowTime() - dateUtils.timeToTimestamp(data[i])) < 60 * 60 * 1000) {
            that.data.status[i] = 1;
          } else {
            that.data.status[i] = 0;
          }
        }
        that.setData({
          data: data,
          times: that.data.times,
          status: that.data.status
        });
        that.initStatus();
        that._requestGoodList(callback);
      }
    });
  },

  /**
   * 请求商品列表
   */
  _requestGoodList: function (callback) {
    if (!this.data.hasNextPage) {
      typeof callback == 'function' && callback();
      return;
    }
    let that = this;
    service.getRobList(5, 0, that.data.startPage, that.data.data[that.data.selectIdx], function (data) {
      if (data.items) {
        if (that.data.startPage == 0) {
          that.setData({
            goods: data.items
          })
        } else {
          that.setData({
            goods: that.data.goods.concat(data.items)
          });
        }
        if (data.hasNextPage) {
          that.setData({
            startPage: startPage++,
          });
        }
        that.setData({
          hasNextPage: data.hasNextPage
        });
      }
      typeof callback == 'function' && callback();
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      startPage: 0,
      hasNextPage: true
    });
    this._requestTimeList(function () {
      console.log('aaaaaaaaa~~!~~~');
      setTimeout(function () {
        wx.stopPullDownRefresh();
      }, 1000);
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this._requestGoodList(function () { });
    }
  },
  //获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },
  /**
    * 用户点击右上角分享
    */
  onShareAppMessage: function (res) {
    var title = this.data.shareMsg;
    title = title.replace(/#/, app.globalData.userInfo.nickName);
    return {
      title: title,
      path: "redPacket/pages/hotExchange/hotExchange?userId=" + app.globalData.userId,
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  },
  /**
   * tab选中
   */
  tabClick: function (e) {
    let idx = e.currentTarget.dataset.idx;
    this.setData({
      selectIdx: idx,
      startPage: 0,
      hasNextPage: true
    })
    this._requestGoodList(function () {
      console.log('hahahaha');
    });
  },
  submitForm: function (e) {
    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId, 10);
  },
  /**
   * 立即抢购
   */
  btnClick: function (e) {
    let idx = e.currentTarget.dataset.idx;
    let goodId = this.data.goods[idx].id;
    let goodType = this.data.goods[idx].saleTypeId;
    wx.navigateTo({
      url: '/goods/pages/goodsDetails/goodsDetails?goods_id=' + goodId + '&goods_type=' + goodType,
    })
  },

  initStatus: function () {
    let idx = 0;
    for (let i = 0; i < this.data.status.length; i++) {
      if (this.data.status[i] == 1 || this.data.status[i] == 0) {
        idx = i;
      }
    }
    this.setData({
      selectIdx: idx
    });
    this.queryMultipleNodes();
  },

  queryMultipleNodes: function () {
    var rpx = 154;
    var systemInfo = wx.getSystemInfoSync();
    var px = rpx / 750 * systemInfo.windowWidth;
    if (this.data.selectIdx - 2 > 0) {
      this.setData({
        scrollTop: px * (this.data.selectIdx - 2)
      })
    }
  }


})